package com.bitbakery.plugin.cl.config;

import com.bitbakery.plugin.cl.lang.CLReplApplicationComponent;
import com.intellij.openapi.util.text.StringUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class CLConfigurationForm {
    private JPanel rootComponent;
    //    private JLabel homeDirectoryLabel;
    private JTextField homeDirectoryTextField;
    private JComboBox commonLispImplementationComboBox;
    private JTextField initializationFileTextField;
    private JButton homeDirectoryChooserButton;
    private JButton initializationFileChooserButton;
    private JButton lispExecutableChooserButton;
    private JTextField lispExecutableTextField;

    public CLConfigurationForm() {
        enableDependentFields();

//        homeDirectoryLabel.setLabelFor(homeDirectoryTextField);
        homeDirectoryChooserButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                JFileChooser chooser = new JFileChooser();
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                pickFile(chooser, CLConfigurationForm.this.homeDirectoryTextField);
                enableDependentFields();
            }
        });
        initializationFileChooserButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                pickFile(new JFileChooser(), CLConfigurationForm.this.initializationFileTextField);
            }
        });
        lispExecutableChooserButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                pickFile(new JFileChooser(), CLConfigurationForm.this.lispExecutableTextField);
            }
        });
    }

    private void enableDependentFields() {
        boolean isEnabled = !StringUtil.isEmptyOrSpaces(homeDirectoryTextField.getText());
        initializationFileTextField.setEnabled(isEnabled);
        initializationFileChooserButton.setEnabled(isEnabled);
        lispExecutableTextField.setEnabled(isEnabled);
        lispExecutableChooserButton.setEnabled(isEnabled);
    }

    private void pickFile(JFileChooser chooser, JTextField textField) {
        if (StringUtil.isNotEmpty(homeDirectoryTextField.getText())) {
            chooser.setCurrentDirectory(new File(homeDirectoryTextField.getText()));
        }
        if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(rootComponent)) {
            textField.setText(chooser.getSelectedFile().getPath());
        }
    }

    public JPanel getRootComponent() {
        return rootComponent;
    }

    public void setData(CLReplApplicationComponent data) {
        homeDirectoryTextField.setText(data.getHomeDirectory());
        initializationFileTextField.setText(data.getInitializationFile());
        lispExecutableTextField.setText(data.getLispExecutable());
    }

    public void getData(CLReplApplicationComponent data) {
        data.setHomeDirectory(homeDirectoryTextField.getText());
        data.setInitializationFile(initializationFileTextField.getText());
        data.setLispExecutable(lispExecutableTextField.getText());
    }

    public boolean isModified(CLReplApplicationComponent data) {
        if (homeDirectoryTextField.getText() != null ? !homeDirectoryTextField.getText().equals(data.getHomeDirectory()) : data.getHomeDirectory() != null) {
            return true;
        }
        if (initializationFileTextField.getText() != null ? !initializationFileTextField.getText().equals(data.getInitializationFile()) : data.getInitializationFile() != null) {
            return true;
        }
        if (lispExecutableTextField.getText() != null ? !lispExecutableTextField.getText().equals(data.getLispExecutable()) : data.getLispExecutable() != null) {
            return true;
        }
        return false;
    }
}
